package org.dding.msc.service;

import java.io.Writer;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

/**
 * A service container which manages a set of running services.
 * 
 */
public interface ServiceContainer extends ServiceTarget, ServiceRegistry {

	// TODO:该方法需要移除
	public Executor getExecutor();
	
	public Writer getProfileOutput();

	public boolean isShutdown();
	
	public long getStart();

	/**
	 * Stop all services within this container.
	 */
	public void shutdown();

	/**
	 * Determine whether the container is completely shut down.
	 * 
	 * @return {@code true} if shutdown is complete
	 */
	boolean isShutdownComplete();

	/**
	 * Add a terminate listener to this container. The added {@code listener}
	 * will be invoked when this container shutdown process is complete.
	 * 
	 * @param listener
	 *            the listener
	 */
	void addTerminateListener(TerminateListener listener);

	/**
	 * Causes the current thread to wait until the container is shutdown.
	 * 
	 * @throws InterruptedException
	 *             if the current thread is interrupted while waiting
	 */
	void awaitTermination() throws InterruptedException;

	/**
	 * Causes the current thread to wait until the container is shutdown.
	 * 
	 * @param timeout
	 *            the maximum time to wait
	 * @param unit
	 *            the time unit of the {@code timeout} argument
	 * @throws InterruptedException
	 *             if the current thread is interrupted while waiting
	 */
	void awaitTermination(long timeout, TimeUnit unit)
			throws InterruptedException;

	/**
	 * Get the name of this service container.
	 * 
	 * @return the container name
	 */
	String getName();

	boolean removeService(String serviceName);

	boolean startService(String serviceName);

	boolean stopService(String serviceName);

	boolean waitService(String serviceName);

	/**
	 * A listener for notification of container shutdown.
	 * 
	 * @see ServiceContainer#addTerminateListener(TerminateListener)
	 * @see ServiceContainer#shutdown()
	 */
	public interface TerminateListener {
		/**
		 * Notifies this listener that the container is shutdown.<br>
		 * At the moment this listener is requested to handle termination, all
		 * services in the container are stopped and removed.
		 * 
		 * @param info
		 *            information regarding the container shutdown process
		 */
		void handleTermination(Info info);

		/**
		 * Container shutdown information.
		 */
		final class Info {
			private final long shutdownInitiated;
			private final long shutdownCompleted;

			public Info(final long shutdownInitiated,
					final long shutdownCompleted) {
				this.shutdownInitiated = shutdownInitiated;
				this.shutdownCompleted = shutdownCompleted;
			}

			/**
			 * Returns the time the shutdown process was initiated, in
			 * nanoseconds.
			 * 
			 * @return the shutdown initiated time
			 */
			public long getShutdownInitiated() {
				return shutdownInitiated;
			}

			/**
			 * Returns the time the shutdown process was completed, in
			 * nanoseconds.
			 * 
			 * @return the shutdown completed time
			 */
			public long getShutdownCompleted() {
				return shutdownCompleted;
			}
		}
	}
}
